#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "BasicMainWindow.h"
#include "Central/CentralTabWidget.h"
#include "MainMenus/MainMenus.h"
#include "Sides/SidePanel.h"

class QSplitter;
class CentralTabDragProxy;

#define qWindow qobject_cast<MainWindow *>(QWidget::window())

class MainWindow : public BasicMainWindow {
    Q_OBJECT
public:
    explicit MainWindow(const QStringList &filenames, QWidget *parent = nullptr);
    explicit MainWindow(const QList<CentralTab *> &tabs, QWidget *parent = nullptr);
    ~MainWindow();

public:
    inline CentralTab *currentTab() const {
        return m_tabWidget->currentTab();
    }
    inline CentralTab *previousTab() const {
        return m_tabWidget->previousTab();
    }
    inline CentralTab *tabAt(int index) const {
        return m_tabWidget->tabAt(index);
    }
    inline int tabCount() const {
        return m_tabWidget->count();
    }

private:
    void init();
    void destruct();

public:
    static MainWindow *primaryWindow();
    static QList<MainWindow *> windows();

    CentralTabWidget *centralTabWidget() const;

protected:
    static QSet<MainWindow *> s_windows;

protected:
    MainMenus *m_menuBar;
    CentralTabWidget *m_tabWidget;
    SidePanel *m_sidePanel;
    QSplitter *m_splitter;

    CentralTabDragProxy *m_dragProxy;

private:
    bool handleTabCloseRequested(int index);
    void handleTabIndexChanged(int index);
    void handleTabNameChanged(int index, const QString &newName);
    void handleTabPressed(int index, Qt::MouseButton button);

public:
    void closeTab(CentralTab *tab); // Close tab
    void closeTab(int index);

    void reloadTitle();
    void setTitle(const QString &title);

protected:
    void dragEnterEvent(QDragEnterEvent *event) override;
    void dropEvent(QDropEvent *event) override;

    void closeEvent(QCloseEvent *event) override;
};

#endif // MAINWINDOW_H
